---
title: "Figure A.10"
output: 
---

# Figure A.10

# Who's to Blame? Postconflict Violence and Public Attitudes Towards Peace Agreements
# Wyer, Frank. 

#clear environment
```{r clear environment}
rm(list = ls())
```

# uncomment and set working directory to replication archive
# setwd("~/blame_replication")

# Uncomment to install packages if necessary
# install.packages("tidyverse")
# install.packages("estimatr")
# install.packages("cowplot")


#load packages
```{r}
library(tidyverse)
library(estimatr)
library(cowplot)

```

#read in data
```{r}
survey_clean <- read.csv("survey_clean.csv")
```

#reorder Petro affinity variable
```{r reorder petro variable}
survey_clean <- survey_clean %>% 
mutate(petrofav = case_when(
  Q55 == 1 ~ 6,
  Q55 == 2 ~ 5,
  Q55 == 3 ~ 4,
  Q55 == 4 ~ 3,
  Q55 == 5 ~ 2,
  Q55 == 6 ~ 1,
  TRUE ~ NA_real_
  )) # changing scale order so that close (1) is the largest value and far (6) is the smallest
```

#scale Petro affinity variable vs control group mean and se
```{r scale petro variable}
survey_clean$petrofav_scale <- (survey_clean$petrofav - mean(survey_clean$petrofav[survey_clean$treatment == "C"], na.rm = TRUE)) / sd(survey_clean$petrofav[survey_clean$treatment == "C"], na.rm = TRUE)
```

#reorder uribe affinity variable
```{r reorder uribe}
survey_clean <- survey_clean %>% 
mutate(uribefav = case_when(
  Q54 == 1 ~ 6,
  Q54 == 2 ~ 5,
  Q54 == 3 ~ 4,
  Q54 == 4 ~ 3,
  Q54 == 5 ~ 2,
  Q54 == 6 ~ 1,
  TRUE ~ NA_real_
  )) # changing scale order so that close (1) is the largest value and far (6) is the smallest
```

#scale Uribe affinity variable vs control group mean and se
```{r scale uribe}
survey_clean$uribefav_scale <- (survey_clean$uribefav - mean(survey_clean$uribefav[survey_clean$treatment == "C"], na.rm = TRUE)) / sd(survey_clean$uribefav[survey_clean$treatment == "C"], na.rm = TRUE)
```

#estimate effect of treatments on Petro favorability among left-leaning respondents
```{r petro left model}
petro_left_95 <- lm_lin(formula = petrofav_scale ~ treatment, covariates = ~ Q15 + urbandummy + engage_zscale + farc_presence + homratediff + factor(regionname), se_type = "HC2", data = survey_clean %>% filter(Q25 < 4), alpha = 0.05) %>% tidy() %>% filter(term == "treatmentT1" | term == "treatmentT2A" | term == "treatmentT2B") %>% mutate(Ideology = "Left", Outcome = "Left Politician Favorability")
```

#estimate effect of treatments on Petro favorability among right-leaning respondents
```{r petro right model}
petro_right_95 <- lm_lin(formula = petrofav_scale ~ treatment, covariates = ~ Q15 + urbandummy + engage_zscale + farc_presence + homratediff + factor(regionname), se_type = "HC2", data = survey_clean %>% filter(Q25 > 3), alpha = 0.05) %>% tidy() %>% filter(term == "treatmentT1" | term == "treatmentT2A" | term == "treatmentT2B") %>% mutate(Ideology = "Right", Outcome = "Left Politician Favorability")
```

#estimate effect of treatments on Uribe favorability among left-leaning respondents
```{r uribe left model}
uribe_left_95 <- lm_lin(formula = uribefav_scale ~ treatment, covariates = ~ Q15 + urbandummy + engage_zscale + farc_presence + homratediff + factor(regionname), se_type = "HC2", data = survey_clean %>% filter(Q25 < 4), alpha = 0.05) %>% tidy() %>% filter(term == "treatmentT1" | term == "treatmentT2A" | term == "treatmentT2B") %>% mutate(Ideology = "Left", Outcome = "Right Politician Favorability")
```

#estimate effect of treatments on Uribe favorability among right-leaning respondents
```{r uribe right model}
uribe_right_95 <- lm_lin(formula = uribefav_scale ~ treatment, covariates = ~ Q15 + urbandummy + engage_zscale + farc_presence + homratediff + factor(regionname), se_type = "HC2", data = survey_clean %>% filter(Q25 > 3), alpha = 0.05) %>% tidy() %>% filter(term == "treatmentT1" | term == "treatmentT2A" | term == "treatmentT2B") %>% mutate(Ideology = "Right", Outcome = "Right Politician Favorability")
```

#dataframe for results of all four models
```{r}
pol_df <- rbind(petro_left_95, petro_right_95, uribe_right_95, uribe_left_95)
```

#relabel and relevel variables for plotting 
```{r prep plot data}
pol_df$Ideology <- factor(pol_df$Ideology, levels = c("Left", "Right"))

pol_df <- pol_df %>% mutate(Treatment = case_when(
term == "treatmentT1" ~ "Postconflict Violence",
term == "treatmentT2B" ~ "Rebel Culpability",
term == "treatmentT2A" ~ "Govt. Culpability",
))

pol_df$Treatment <- factor(pol_df$Treatment, levels = c("Postconflict Violence", "Rebel Culpability", "Govt. Culpability"))
```

#plot treatment effects on Petro affinity
```{r petro plot}
petro_fav_plot <- ggplot(pol_df %>% filter(Outcome == "Left Politician Favorability"),  aes(x = estimate, y = Ideology)) +
    geom_point(position=position_dodge(.5), shape = 15) +
    geom_linerange(aes(y = Ideology, xmin = conf.low, xmax = conf.high), position=position_dodge(.5)) +
    labs(x = "", y = "", title = "Outcome: Left-wing Politician (Petro) Favorability") +
    theme_bw() + 
    xlim(-.75,.75) +
    guides(color = "none", shape = "none") +
          scale_y_discrete(labels = function(x) str_wrap(x, width = 10)) +
    theme(plot.title = element_text(face = "bold", size = 10)) +
    facet_grid(Treatment ~ ., labeller = label_wrap_gen(width = 15, multi_line = TRUE))
```

#plot treatment effects on Uribe affinity
```{r uribe plot}
uribe_fav_plot <- ggplot(pol_df %>% filter(Outcome == "Right Politician Favorability"),  aes(x = estimate, y = Ideology)) +
    geom_point(position=position_dodge(.5), shape = 15) +
    geom_linerange(aes(y = Ideology, xmin = conf.low, xmax = conf.high), position=position_dodge(.5)) +
    labs(x = "", y = "", title = "Outcome: Right-wing Politician (Uribe) Favorability") +
    theme_bw() + 
    xlim(-.75,.75) +
    guides(color = "none", shape = "none") +
          scale_y_discrete(labels = function(x) str_wrap(x, width = 10)) +
    theme(plot.title = element_text(face = "bold", size = 10)) +
    facet_grid(Treatment ~ ., labeller = label_wrap_gen(width = 15, multi_line = TRUE))
```

#combine plots
```{r}
pol_plot_all <- plot_grid(uribe_fav_plot, petro_fav_plot, nrow = 1, ncol = 2)
```

